<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Banner;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BannerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Banner(), function (Grid $grid) {
            $grid->model()->orderBy('order');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('sign')->label();
            $grid->column('image')->display(function ($banner) {
                return '<img src="' . $banner . '" width="150">';
            });
            $grid->column('valid')->switch();
            $grid->column('order')->orderable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->like('title')->width(3);
                $filter->equal('sign')->width(3)->select('api/banner_signs');
                $filter->equal('valid')->select([0 => '否', 1 => '是'])->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Banner(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('sign');
            $show->field('image')->image();
            $show->field('link_type');
            $show->field('link');
            $show->field('valid')->using([0 => '否', 1 => '是']);
            $show->field('order');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Banner(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('sign')->options('/api/banner_signs')->required();
            $form->image('image')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
            $form->select('link_type')->options('/api/link_types')->required();
            $form->text('link')->required();
            $form->switch('valid');
            // $form->number('order')->required();
        });
    }
}
